package cn.mogugo.joke.action;

import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import cn.mogugo.comm.BaseAction;
import cn.mogugo.joke.model.JokeObj;
import cn.mogugo.util.tools.DateTools;

public class JokeKeywordAction  extends BaseAction{
	/**
	 * 默认方法
	 */
	public ActionForward unspecified(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
	
		return index(mapping,form,request,response);
	}
	
	
	public ActionForward index(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		return mapping.findForward("index");
	}
	

	public ActionForward saveKeyword(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		List list = this.jokeObjManager.getAllNoKeyword();
		System.out.println(list.size());
		for(int i = 0 ; i < list.size() ; i ++ ){
			JokeObj jo = (JokeObj)list.get(i);
			String strContent = jo.getJoke_content().replaceAll("\\s+", "").replaceAll("<[a-z]+>", "").replaceAll("\\d+", "").replaceAll("\\w+", "");
			
			
			Reader reader = new StringReader(strContent);
			
			IKSegmenter iks = new IKSegmenter(reader , true); 
			
			Lexeme lex=null;
			String tmp = " ";
			while((lex=iks.next())!=null){
				String at = lex.getLexemeText();
				if(tmp.indexOf((" "+at+"")) == -1){
					tmp += at + " ";
				}
			}
			
			jo.setKey_words(tmp);
			this.baseDao.saveOrUpdate(jo);
		}

		
		return mapping.findForward("toIndex");
	}
	
	public ActionForward saveJokeDate(
            ActionMapping mapping,
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		List list = this.jokeObjManager.getBefore20121001();
		
		System.out.println(list.size());

		
		Date beginDate = DateTools.stringToDate("2012-09-30 12:00:00" , DateTools.FULL_YMDHMS);
		Calendar c = Calendar.getInstance();
		c.setTime(beginDate);
		List listNew = new ArrayList();
		
		
		for(int i = 0 ; i < list.size() ; i ++ ){
			JokeObj jo = (JokeObj)list.get(i);
			c.add(Calendar.MINUTE, -1);
			jo.setJoke_date(c.getTime());
			if(i % 200 > 0){
				listNew.add(jo);
			}else{
				if(!listNew.isEmpty()){
					this.baseDao.saveOrUpdateFlush(listNew);
					listNew.clear();
				}
			}
		}
		if(!listNew.isEmpty()){
			this.baseDao.saveOrUpdateFlush(listNew);
			listNew.clear();
		}
		
		this.saveRequestMsg(request , list.size()+"完成");
		return mapping.findForward("toIndex");
	}
}
